package com.oa.cms.controller;

import com.oa.cms.controller.api.CmsVersionApi;
import com.oa.cms.converter.CmsVersionConverter;
import com.oa.cms.domain.dto.CmsVersionDto;
import com.oa.cms.domain.entity.CmsVersion;
import com.oa.cms.domain.enums.Status;
import com.oa.cms.query.CmsVersionQuery;
import com.oa.cms.query.base.Page;
import com.oa.cms.service.CmsVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by gold-jx on 2017/12/1.
 */
@RestController
@Api(description = "CMS -> 版本管理")
public class CmsVersionApiController implements CmsVersionApi{


    @Autowired
    private CmsVersionService cmsVersionService;

    @Autowired
    private CmsVersionConverter cmsVersionConverter;


    @Override
    public ResponseEntity<Void> addCmsVersion(@ApiParam(value = "CMS版本新增", required = true)@RequestBody CmsVersionDto cmsVersionDto) {
        Assert.notNull(cmsVersionDto,"入参不能为空");
        CmsVersion cmsVersion= cmsVersionConverter.convertToEntity(cmsVersionDto);
        cmsVersionService.save(cmsVersion);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Override
    public ResponseEntity<Void> updateCmsVersion(@ApiParam(value = "CMS版本修改", required = true)@RequestBody CmsVersionDto cmsVersionDto) {
        CmsVersion cmsVersion= cmsVersionConverter.convertToEntity(cmsVersionDto);
        Assert.notNull(cmsVersion.getId(),"修改ID不能为空");
        cmsVersionService.update(cmsVersion);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Override
    public ResponseEntity<Void> deleteRealCmsVersion(@ApiParam(value = "CMS版本真删除", required = true) @RequestBody String id) {
        Assert.notNull(id,"id不能为空");
        long id1;
        try {
             id1=Long.parseLong(id);
            cmsVersionService.delete(id1);
        }catch (Exception e){
            Assert.isTrue(true,"id传入值异常");
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Override
    public ResponseEntity<Void> deleteFalseCmsVersion(@ApiParam(value = "CMS版本假删除", required = true) @RequestBody String id) {
        Assert.notNull(id,"id不能为空");
        long id1;
        try {
            id1=Long.parseLong(id);
            CmsVersion cmsVersion=new CmsVersion();
            cmsVersion.setId(id1);
            cmsVersion.setStatus(Status.DELETED);
            cmsVersionService.delete(id1);
        }catch (Exception e){
            Assert.isTrue(true,"id传入值异常");
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);

    }

    @Override
    public ResponseEntity<Page<CmsVersionDto>> getVersion(CmsVersionQuery cmsVersionQuery) {

        Page<CmsVersionDto> cmsVersionDtoPage = cmsVersionService.list(cmsVersionQuery);
        if(!StringUtils.isEmpty(cmsVersionDtoPage.getDatas())){
            List<CmsVersionDto> cmsVersionDtos=cmsVersionDtoPage.getDatas().stream().map(request -> {
                CmsVersionDto cmsVersionDto=new CmsVersionDto();
                cmsVersionDto.setId(request.getId());
                cmsVersionDto.setVersionType(request.getVersionType());
                cmsVersionDto.setVersion(request.getVersion());
                cmsVersionDto.setDownPath(request.getDownPath());
                cmsVersionDto.setCreateTime(request.getCreateTime());
                cmsVersionDto.setStatus(request.getStatus());
                cmsVersionDto.setTips(request.getTips());
                return cmsVersionDto;
            }).collect(Collectors.toList());
            return ResponseEntity.ok(new Page<>(cmsVersionDtoPage.getTotalCount(), cmsVersionDtos));
        }

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
